package com.qf.service.serviceImpl;

import com.qf.dao.OrderMapper;
import com.qf.pojo.BaseResp;
import com.qf.pojo.Orders;
import com.qf.pojo.Vo.OrdersVo;
import com.qf.service.OrderService;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;

    @Override
    public BaseResp selectAll(int uid) {

        List<Orders> orders = orderMapper.selectAll(uid);
        BaseResp baseResp = new BaseResp();

        if(orders!=null){
            baseResp.setCode(200);
            baseResp.setData(orders);
            baseResp.setMgs("查询成功");
            baseResp.setTotal(orders.size());
            return baseResp;
        }
        baseResp.setCode(0);
        baseResp.setMgs("查询失败");
        return baseResp;
    }

    @Override
    public BaseResp selectOne(int id) {
        Orders orders = orderMapper.selectOne(id);
        BaseResp baseResp = new BaseResp();
        if(orders!=null){
            baseResp.setCode(200);
            baseResp.setData(orders);
            baseResp.setMgs("查询成功");
            return baseResp;
        }
        baseResp.setCode(0);
        baseResp.setMgs("查询失败");
        return baseResp;

    }

    @Override
    public BaseResp add(OrdersVo vo) {
        Orders orders = new Orders();
        orders.setUid(vo.getUid());
        orders.setPid(vo.getPid());
        orders.setNum(vo.getNum());
        orders.setPrice(vo.getPrice());
        orders.setTime(new Date());

        int add = orderMapper.add(orders);
        BaseResp baseResp = new BaseResp();
        if(add==1){
            baseResp.setMgs("添加成功");
            baseResp.setCode(200);
            baseResp.setData(orders.getId());
            return baseResp;
        }
        baseResp.setCode(0);
        baseResp.setMgs("添加失败");
        return baseResp;
    }

    @Override
    public BaseResp delete(int id) {

        int num = orderMapper.delete(id);
        BaseResp baseResp = new BaseResp();
        if(num==1){
            baseResp.setMgs("删除成功");
            baseResp.setCode(200);
            return baseResp;
        }
        baseResp.setCode(0);
        baseResp.setMgs("删除失败");

        return baseResp;
    }

    @Override
    public BaseResp update(OrdersVo vo) {



        int id = vo.getId();
        int uid = vo.getUid();
        int pid = vo.getPid();
        String num = vo.getNum();
        String price = vo.getPrice();
        String state = vo.getState();
        String evaluation = vo.getEvaluation();
        String o = vo.getO();
        int level = vo.getLevel();
        Orders orders = new Orders(id,uid,pid,"num","price","state","evaluation",o,level);

        int update = orderMapper.update(orders);
        BaseResp baseResp = new BaseResp();
        if(update==1){
            baseResp.setCode(200);
            baseResp.setMgs("修改成功");
            return  baseResp;
        }
            baseResp.setCode(0);
            baseResp.setMgs("修改失败");
        return baseResp;
    }


    @Override
    public BaseResp findOne(int id) {
        Orders orders = orderMapper.findOne(id);
        BaseResp baseResp = new BaseResp();

        if(orders!=null){
        baseResp.setCode(200);
        baseResp.setMgs("查询成功");
        baseResp.setData(orders);
        return baseResp;
        }
        baseResp.setCode(0);
        baseResp.setMgs("查询失败");
        return baseResp;
    }

    @Override
    public BaseResp selectPlayAll(int pid) {
        List<Orders> orders = orderMapper.selectPlayAll(pid);
        BaseResp baseResp = new BaseResp();
        if(orders!=null){
            baseResp.setCode(200);
            baseResp.setMgs("查询成功");
            baseResp.setData(orders);
            return baseResp;
        }
        baseResp.setCode(0);
        baseResp.setMgs("查询失败");
        return baseResp;
    }

    @Override
    public BaseResp findByEvaluation(String evaluation) {
        BaseResp baseResp = new BaseResp();
        Orders byEvaluation = orderMapper.findByEvaluation(evaluation);
        baseResp.setCode(200);
        baseResp.setData(byEvaluation);
        return baseResp;
    }


}
